package com.gljx.web.controller;

import com.gljx.util.CommonExport;
import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.Positiongrade;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.PositiongradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 人力资源基础资料
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/")
public class PositiongradeController {
	// 自动注入
	@Autowired
	private DepartmentService departmentService;

	@Autowired
	private CustomLogService customLogService;
	
	@Autowired
	private PositiongradeService positiongradeService;
	
	/**
	 * 查询职级职等信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selectPositiongrade.html/look.action")
	public String selPositiongrade(HttpServletRequest request,@RequestParam(value = "type",required = false,defaultValue = "") String type)throws Exception{
		//获取session中的登录信息
        User user = (User) request.getSession().getAttribute("user");
		//查询扣款的项目
        List<Positiongrade> positiongradeList=null;
        positiongradeList=positiongradeService.selectPosttiongradeList(user.getFcompanyid());
        request.setAttribute("positiongradeList", positiongradeList);
        request.setAttribute("type",type);
		return "backHome/basicData/humanResourceData/positiongrade/positiongradeList";
	}
	/**
	 * 根据名称查询职级职等信息
	 * @param request
	 * @param fnameL2
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/selectPositiongradeByFnameL2.html")
	public String selectPositiongradeByFname(HttpServletRequest request,String fnameL2)throws Exception{
		//获取session中的登录信息
        User user = (User) request.getSession().getAttribute("user");
		//查询扣款的项目
        List<Positiongrade> positiongradeList=null;
        positiongradeList=positiongradeService.selectPositiongradeByFname(fnameL2, user.getFcompanyid());
        request.setAttribute("positiongradeList", positiongradeList);
        request.setAttribute("fnameL2", fnameL2);
		return "backHome/basicData/humanResourceData/positiongrade/positiongradeList";
	}
	/**
	 * 跳转到增加职级职等的页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoAddPositiongrade.html")
	public String intoAddPositiongrade(HttpServletRequest request,String type)throws Exception{
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取部门
	    List<Department>departments = departmentService.queryAdminByController(user.getFcompanyid());
	    //获取组织
	    Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	  //获取上一个编码
        /*String number = positiongradeService.selectFnumber(user.getFcompanyid());*/
        //获取编码
        String fnumber = GetFnumber.fnumbers("SFPT");
	    request.setAttribute("department_positiongrade", department);
	    request.setAttribute("departments_positiongrade", departments);
	    request.setAttribute("fnumber", fnumber);
	    request.setAttribute("type",type);
		return "backHome/basicData/humanResourceData/positiongrade/addPositiongrade";
	}
	/**
	 * 增加职级职等信息
	 * @param request
	 * @param positiongrade 需要新增加的职级职等信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/addPositiongrade.html")
	public String addPositiongrade(HttpServletRequest request,Positiongrade positiongrade)throws Exception{
		//获取操作人信息
        User user = (User) request.getSession().getAttribute("user");
        /*获取当前时间*/
        String createTime = TimeFormat.timeFormat(new Date());
        /*获取uuid*/
        positiongrade.setFid(GetFid.UUIDIntoFid());
        positiongrade.setCfficompanyid(user.getFcompanyid());
        positiongrade.setFcreatorid(user.getFid());
        positiongrade.setFcreatetime(createTime);
        positiongrade.setFcontrolunitid(user.getFcompanyid());
        positiongrade.setCreateName(user.getName());
        int row = positiongradeService.addPositionggrade(positiongrade);
        String result ="";
        if(row > 0){
        	result ="1";
		}
        customLogService.addLog("人力资源基础资料", createTime + "对职级职等进行增加操作", null, user.getFid());
		return "redirect:/DrivingSys/intoAddPositiongrade.html?type="+result;
	}
	/**
	 * 跳转到修改职级职等页面
	 * @param request
	 * @param fid 根据fid把需要修改的职级职等信息带到修改页面中去
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/intoUpdatePositiongrade.html")
	public String intoUpdatePositiongrade(HttpServletRequest request,
			@RequestParam(value="fid",required=false) String fid)throws Exception{
		//获取当前登录人的信息
		User user = (User) request.getSession().getAttribute("user");
		// 根据fid查询单条扣款项目信息带到修改页面中去
		Positiongrade positiongrade = null;
		if (fid != null && !fid.equals("")) {
			positiongrade = positiongradeService.queryPositiongradeByFid(fid);
		}
		// 获取部门
		List<Department> departments = departmentService.queryAdminByController(user.getFcompanyid());
		// 获取组织
		Department department = departmentService.queryAdminByFid(user.getFcompanyid());
		request.setAttribute("department_positiongrade", department);
		request.setAttribute("departments_positiongrade", departments);
		request.setAttribute("update_positiongrade", positiongrade);
		return "backHome/basicData/humanResourceData/positiongrade/updatePositiongrade";
	}
	/**
	 * 修改职级职等的信息
	 * @param request
	 * @param positiongrade 需要修改的信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/updatePositigrade.html")
	public String updatePositiongrade(HttpServletRequest request,Positiongrade positiongrade)throws Exception{
		//获取当前登录人的信息
		User user = (User) request.getSession().getAttribute("user");
		//公司id
		positiongrade.setCfficompanyid(user.getFcompanyid());
		 //控制单元id
		positiongrade.setFcontrolunitid(user.getFcompanyid());
		//获取当前修改时间
		String updateTime = TimeFormat.timeFormat(new Date());
		//设置最后修改时间
		positiongrade.setFlastupdatetime(updateTime);
		//设置修改人id
		positiongrade.setFlastupdateuserid(user.getFid());
		//修改
		int row = positiongradeService.updatePositiongradeByFid(positiongrade);
		String result="";
		if(row > 0){
			result ="1";
		}
		//添加日志
		customLogService.addLog("人力资源基础资料", TimeFormat.timeFormat(new Date()) + "对职级职等进行更新", null, user.getFid());
		return "redirect:/DrivingSys/selectPositiongrade.html/look.action?type="+result;
	}
	/**
	 * 根据fid删除职级职等信息
	 * @param request
	 * @param deleteId 需要删除的职级职等信息的fid数组
 	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deletePositiongrade.html")
	public String deletePositiongrade(HttpServletRequest request, String[] deleteId,String fid)throws Exception{
		User user = (User) request.getSession().getAttribute("user");
        //删除记录
		if (fid == null || fid.equals("")) {
			for (int i = 0; i < deleteId.length; i++) {
				/* 过滤为空的删除操作 */
				if (!deleteId[i].equals("") && deleteId[i] != null) {
					positiongradeService.deletePositiongradeByFid(deleteId[i]);
				}
			}
		} else {
			positiongradeService.deletePositiongradeByFid(fid);
		}
        
        //添加日志
        customLogService.addLog("人力资源基础资料", TimeFormat.timeFormat(new Date()) + "对职级职等进行删除", null, user.getFid());
		return "redirect:/DrivingSys/selectPositiongrade.html/look.action";
	}

	/**
	 * 基础资料 人力资源 职级职等 导出
	 * @param response
	 * @param request
	 * @param fnameL2
	 */
	@RequestMapping(value = "/importPositiongrade")
	public void importPositiongrade(HttpServletResponse response, HttpServletRequest request, String fnameL2){
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		//定义接收类型
		List<Object> data = new ArrayList<>();
		//查询数据
		List<Positiongrade> positiongrades =positiongradeService.selectPositiongradeByFname(fnameL2,user.getFcompanyid());
		for (Positiongrade positiongrade : positiongrades){
			data.add(positiongrade.getFnumber());
			data.add(positiongrade.getFnameL2());
			data.add(positiongrade.getFsimplename());
			data.add(positiongrade.getFdescriptionL2());
			data.add(positiongrade.getFcreatetime());
			data.add(positiongrade.getFlastupdatetime());
			data.add(positiongrade.getCreateName());
			data.add(positiongrade.getUpdateName());
		}
		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "创建时间", "最后修改时间", "创建者", "修改人"};
		//长度
		Integer length[] = {9000, 5000, 5000, 5000, 5000, 5000, 3000, 3000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "职级职等" + TimeFormat.getNowTime2() + ".xls", "职级职等", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
